/***********************************************
 * Title:       TeacherService.java
 * Description: TeacherService.java
 * Author:      stu
 * Create Date: 2010-3-16
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.teacherManagement;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.infohazard.maverick.flow.ControllerContext;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.younker.waf.core.user.User;
import com.younker.waf.core.user.UserService;
import com.younker.waf.db.IBatisConfig;

/**
 * @author stu
 * 
 */
public class TeacherService
{

        public final static long NULL_TEACHER_ID = -1;

        private Logger log = Logger.getLogger(this.getClass());

        private static Teacher NULL_TEACHER = new Teacher();
        
        static
        {
                NULL_TEACHER.setId(NULL_TEACHER_ID);
        }

        private TeacherService(){}

        private static TeacherService _instance;

        static
        {
                _instance = new TeacherService();
        }

        public static TeacherService getInstance()
        {
                return _instance;
        }

        protected SqlMapClient sqlMap = IBatisConfig.getSqlMapInstance();

        public Teacher getNullTeacher()
        {
                return NULL_TEACHER;
        }

        public Teacher getTeacherById(long id)
        {
                try
                {
                        return (Teacher) sqlMap.queryForObject("getTeacherById", id);
                } catch (SQLException e)
                {
                        log.error("get Teacher error[id=" + id + "].", e);
                        return getNullTeacher();
                }
        }

        @SuppressWarnings("unchecked")
        public List<Teacher> getTeacherAnd(Teacher teacher)
        {
                try
                {
                        return sqlMap.queryForList("getTeacherAnd", teacher);
                } catch (SQLException e)
                {
                        log.error("getTeahcerAnd error:" + teacher, e);
                        return new ArrayList<Teacher>();
                }
        }

        @SuppressWarnings("unchecked")
        public List<Teacher> getTeacherOr(Teacher teacher)
        {
                try
                {
                        return sqlMap.queryForList("getTeacherOr", teacher);
                } catch (SQLException e)
                {
                        log.error("getTeahcerOr error:" + teacher, e);
                        return new ArrayList<Teacher>();
                }
        }

        public long insertTeacher(Teacher teacher)
        {
                try
                {
                        return (Long) sqlMap.insert("insertTeacher", teacher);
                } catch (SQLException e)
                {
                        log.error("insert teacher error:" + teacher, e);
                        return 0;
                }
        }

        public long updateTeacher(Teacher teacher)
        {
                try
                {
                        return (Integer) sqlMap.update("updateTeacher", teacher);
                } catch (SQLException e)
                {
                        log.error("update Teacher error:" + teacher, e);
                        return 0;
                }

        }

        public void deleteTeacher(long id)
        {
                try
                {
                        sqlMap.delete("deleteTeacher", id);
                } catch (SQLException e)
                {
                        log.error("delete teacher error", e);
                }
        }
        
        public Teacher getCurrentTeacher( ControllerContext ctx )
        {
                User user = null;
                Teacher teacher = null;
                try
                {
                        user = UserService.getInstance().getCurrentUser(ctx);
                        
                        if( user == null )
                        {
                                return getNullTeacher();
                        }
                        String teacherNo = user.getEmpID();
                        
                        if( teacherNo == null )
                                return NULL_TEACHER;
                        teacher = new Teacher();
                        teacher.setTeaNo(teacherNo);
                        teacher = (Teacher) sqlMap.queryForObject("getTeacherAnd", teacher);
                            
                        if( teacher == null )
                                teacher = NULL_TEACHER;
                        
                        
                } catch (SQLException e)
                {
                        log.error("get teacher error", e);
                }
                return teacher;
        }

}
